﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Un4seen.Bass;
using System.Runtime.InteropServices;

namespace ASpect.Logic
{
    public class Audio
    {
        #region Локальные переменные
        int _device, _freq, _stream;
        float[] _out=new float[128];
        bool _playmethod = false;
        #endregion

        #region Конструкторы
        public Audio(int device, int freq)
        {
            _freq=freq;
            _device=device;
        }
        #endregion

        #region Публичные методы
        public bool Start(System.Windows.Forms.Form form)
        {
            return Bass.BASS_Init(_device, _freq, BASSInit.BASS_DEVICE_DEFAULT, form.Handle);
        }

        public void Play()
        {
            Bass.BASS_ChannelPlay(_stream,_playmethod);
        }

        public void Stop()
        {
            Bass.BASS_ChannelStop(_stream);
            _playmethod = true;
        }

        public void Pause()
        {
            Bass.BASS_ChannelPause(_stream);
            _playmethod = false;
        }

        public bool LoadFile(string file)
        {
            _stream = Bass.BASS_StreamCreateFile(file, 0, 0, BASSFlag.BASS_STREAM_PRESCAN);
            if (_stream!=0)
                return(true);
            else
                return(false);
        }

        public float[] FFTData()
        {
            GCHandle hGC = GCHandle.Alloc(_out, GCHandleType.Pinned);
            Bass.BASS_ChannelGetData(_stream, hGC.AddrOfPinnedObject(), (int)BASSData.BASS_DATA_FFT256);
            return _out;
        }

        public bool PlaybackStat()
        {
            return (Bass.BASS_ChannelIsActive(_stream) == BASSActive.BASS_ACTIVE_PLAYING);
        }

        public string MusicInfo(string safefilename,int length,int i)
        {
            BASS_CHANNELINFO chinf;
            chinf = Bass.BASS_ChannelGetInfo(_stream);
            return "File: " + (safefilename.Length>length ? ((safefilename.Length-i)>length ? safefilename.Substring(i,length):safefilename.Substring(i)) : safefilename) + "\n" + "Number of channels: " + chinf.chans.ToString() + "\n" + "Frequency: " + chinf.freq + "Hz";
        }
        #endregion

        #region Публичные переменные
        public int Stream
        {
            get
            {
                return _stream;
            }
        }
        #endregion
    }
}
